'Test suite for LCDI2CLib


'Initialise the I2C LCD device
LCDI2C_Init(&H27)
WaitForAKey("Press any key for next test")

'Turn On the Backlight
LCDI2C_BacklightOn
WaitForAKey("Press any key for next test")

'Turn OFF the Backlight
LCDI2C_BacklightOff
WaitForAKey("Press any key for next test")

'Turn On the Backlight
LCDI2C_BacklightOn
WaitForAKey("Press any key for next test")

LCDI2C 1,3,Date$
WaitForAKey("Press any key for next test")

LCDI2C 2,4,Time$
WaitForAKey("Press any key for next test")

LCDI2C_Clear
WaitForAKey("Press any key for next test")

Do

' L=TIMER 'DEBUG
LCDI2C 1,3,Date$
LCDI2C 2,4,Time$
' PRINT TIMER-L 'DEBUG

Loop

End

Sub WaitForAKey(Msg$)
If Msg$="" Then Msg$="Press any key to continue"
Print Msg$;
Do
Loop While Inkey$=""
Print
End Sub

'----------------------------------------------------------- -------
'
' LCDI2CLib V1 2014-July-07
'
' Micromite to LCD using I2C module
'
' (c) Peter Carnegie 2014
'
' This code is a heavily modified version of I2CLCD.BAS by John Gerrard
'
'
' Uses I2C -> LCD Interface from eBay priced about US$1.50
' http://www.ebay.co.uk/itm/291116790762?ssPageName=STRK:MEWNX :IT&_trksid=p3984.m1439.l2649
' http://www.youtube.com/watch?v=wmTWk4Rwfw0
'
' Uses PCF8574 - 8 bit I2C Port Expander
' I2CAddress is &H27 with unjumpered address pads
' PCF8574 port is wired as follows
' P0 - RS
' P1 - RW
' P2 - EN
' P3 - Backlight On/Off
' P4 - D4 LCD
' P5 - D5 LCD
' P6 - D6 LCD
' P7 - D7 LCD

' Functions in the Library are modelled after the native MMBasic LCD commands
' to ease code conversion
'
' Sub LCDI2C(LineNum,CharPos,Text$) Display string Text$ on LineNum of the display starting at CharPos
' Sub LCDI2C_Clear() Clears the LCD
' Sub LCDI2C_Init(I2CAddr) Initialise the I2C and LCD
' Sub LCDI2C_Close() Close the I2C LCD

' SUB LCDI2C_BacklightOn() Turn LCD backlight ON
' SUB LCDI2C_BacklightOff() Turn LCD backlight OFF

' SUB LCDI2C_CMD(Byte) Send a COMMAND byte to the LCD, ie RS=0
' SUB LCDI2C_DATA(Byte) Send a DATA byte to the LCD, ie RS=1
'
' SUB LCDI2C_DirectSend(Byte) Send a BYTE to the LCD - used internally by the lib
' SUB LCDI2C_WireWrite(Byte) Write a byte onto the I2C bus to the LCD
'
'
' Uses 5 Global Variables
' LCDI2C_LCDBackLight, LCDI2C_I2CAddr, LCDI2C_RSDataMask, LCDI2C_EMask, LCDI2C_BackLight
'



'----------------------------------------------------------- -------
'
' Print String to LCD
'
'
Sub LCDI2C(LineNum,CharPos,Text$)
Local I
If LineNum=1 Then I=(&H80 + CharPos-1) 'I=&H02
If LineNum=2 Then I=(&HC0 + CharPos-1)
LCDI2C_CMD(I)

For I=1 To Len(Text$)
LCDI2C_DATA(Asc(Mid$(Text$,I,1)))
Next I

End Sub


'----------------------------------------------------------- -------
'
' Clear LCD
'
'
Sub LCDI2C_Clear()
LCDI2C_CMD(&H01)
End Sub


'----------------------------------------------------------- -------
'
' INITIALIZE LCD
'
'
Sub LCDI2C_Init(I2CAddr)

'Current Backlight state
Dim LCDI2C_LCDBackLight
LCDI2C_LCDBackLight=0

Dim LCDI2C_I2CAddr
LCDI2C_I2CAddr=I2CAddr

Dim LCDI2C_RSDataMask
'Select Data register = High P0 on 8574
LCDI2C_RSDataMask=&B00000001

Dim LCDI2C_EMask
'Enable = P2 on 8574
LCDI2C_EMask=&B00000100

Dim LCDI2C_BackLight
'Backlight = P3
LCDI2C_BackLight = &B00001000

'Start I2C
I2C1 OPEN 400,100

'%0011---- %0011---- 8-bit / 8-bit
LCDI2C_CMD(&H33)
'%0011---- %0010---- 8-bit / 4-bit
LCDI2C_CMD(&H32)

' Byte commands - To configure the LCD

' Display Format
' 4bit mode, 2 lines, 5x7
'
' 001LNF00
' %00101000
LCDI2C_CMD(&H28)

' L : 0 = 4-bit Mode 1 = 8-bit Mode
' N : 0 = 1 Line 1 = 2 Lines
' F : 0 = 5x7 Pixels 1 = N/A


' Setup Display
' Display ON, Cursor On, Cursor Steady
'
' 00001DCB
' %00001110
LCDI2C_CMD(&H0C)

' D : 0 = Display Off 1 = Display On
' C : 0 = Cursor Off 1 = Cursor On
' B : 0 = Cursor Steady 1 = Cursor Flash


' Setup Cursor/Display
' Inc Cursor Cursor Move
'
' 000001IS
LCDI2C_CMD(&H06)
' I : 0 = Dec Cursor 1 = Inc Cursor
' S : 0 = Cursor Move 1 = Display Shift

LCDI2C_CMD(&H01)

'Turn Off LCDBacklight
LCDI2C_BackLightOff()

End Sub


'----------------------------------------------------------- -------
'
' Close the I2C LCD
'
'
Sub LCDI2C_Close()

'Close the I2C Bus
I2C1 CLOSE

End Sub


'----------------------------------------------------------- -------
'
' Turn Backlight On
'
'
Sub LCDI2C_BacklightOn
LCDI2C_LCDBacklight=LCDI2C_Backlight
LCDI2C_WireWrite(0)
End Sub


'----------------------------------------------------------- -------
'
' Turn Backlight Off
'
'
Sub LCDI2C_BacklightOff
LCDI2C_LCDBackLight=&H00
LCDI2C_WireWrite(0)
End Sub


'----------------------------------------------------------- -------
'
' Send Command Byte to LCD
'
'
Sub LCDI2C_CMD(Byte)

LCDI2C_DirectSend(Byte And &HF0)

LCDI2C_DirectSend((Byte And &H0F) * 16)

End Sub


'----------------------------------------------------------- -------
'
' Send Data Byte to LCD
'
'
Sub LCDI2C_DATA(Byte)

LCDI2C_DirectSend((Byte And &HF0) Or LCDI2C_RSDataMask)

LCDI2C_DirectSend(((Byte And &H0F) * 16) Or LCDI2C_RSDataMask)

End Sub


'----------------------------------------------------------- -------
'
' Send Byte to LCD over I2C
'
'
Sub LCDI2C_DirectSend(Byte)
Local B

'Take EN high
B=Byte Or LCDI2C_EMask Or LCDI2C_LCDBacklight
I2C1 WRITE LCDI2C_I2CAddr,0,1,B

'Take EN Low
I2C1 WRITE LCDI2C_I2CAddr,0,1,Byte Or LCDI2C_LCDBacklight

End Sub


'----------------------------------------------------------- -------
'
' Send Byte over I2C
'
'
Sub LCDI2C_WireWrite(Byte)
I2C1 WRITE LCDI2C_I2CAddr,0,1,Byte Or LCDI2C_LCDBacklight
End Sub                                                                              